﻿using famails.App_Code.source.dto;
using famails.bus;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.Customer
{
    public partial class ProductList : System.Web.UI.Page
    {
        static log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                //LoadData();
            }
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }
        
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<CM_ProductDTO> Search(string name, string code, int groupid, string from, string to, decimal pfrom, decimal pto)
        {
            userLogin = (UserLoginDTO)HttpContext.Current.Session["us-login"];
            List<CM_ProductDTO> result = new List<CM_ProductDTO>(); 
            try
            {
                //string name = "", phone = "h", email = ""; int group = -1;
                CM_ProductBUS ctBUS = new CM_ProductBUS();
                DataTable ProductBySelect = ctBUS.Search(name, code, groupid, from, to, pfrom, pto, userLogin.UserId);
                CM_ProductDTO item = new CM_ProductDTO();
                foreach (DataRow dr in ProductBySelect.Rows)
                {
                    item = new CM_ProductDTO();
                    item.ProductID = Convert.ToInt32(dr["ProductID"] + "");
                    item.ProductName = dr["ProductName"] + "";
                    item.Price = Convert.ToDecimal(dr["Price"] + "");
                    if (dr["GroupID"]+""!="")
                        item.GroupID = Convert.ToInt32(dr["GroupID"] + "");
                    item.GroupName = dr["GroupName"] + "";
                    item.ProductCode = dr["ProductCode"] + "";
                    if (dr["CreateDate"] + "" != "")
                        item.CreateDate = Convert.ToDateTime(dr["CreateDate"] + "");
                    result.Add(item);
                }
            }
            catch (Exception ex)
            {
                logs.Error("Product - DeleteEmail", ex);
            }
            return result;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Delete(int id)
        {
            try
            {                
                CM_ProductBUS ctBUS = new CM_ProductBUS();
                if (ctBUS.CheckBeforeDelete(id))
                    ctBUS.Delete(id);
                else throw new Exception("Sản phẩm này đã được mua hàng, bạn không thể xóa!");
            }
            catch (Exception ex)
            {
                logs.Error("Product - DeleteEmail", ex);
                throw ex;
            }
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<CM_ProductGroupDTO> LoadTree()
        {
            List<CM_ProductGroupDTO> result = new List<CM_ProductGroupDTO>();

            CM_ProductGroupBUS bus = new CM_ProductGroupBUS();
            DataTable source = bus.GetProductGroup(userLogin.UserId, 1);

            CM_ProductGroupDTO dto = new CM_ProductGroupDTO();
            dto.ProductGroupName = "(Tất cả)";
            dto.ProductGroupID = -1;
            dto.children = new List<CM_ProductGroupDTO>();
            result.Add(dto);
            foreach (DataRow row in source.Rows)
            {
                dto = new CM_ProductGroupDTO();
                dto.ProductGroupName = row["ProductGroupName"] + "";
                dto.ProductGroupID = Convert.ToInt32(row["ProductGroupID"]);
                dto.CreateDate = Convert.ToDateTime(row["CreateDate"]);
                dto.children = GetChildGroup(dto.ProductGroupID, Convert.ToInt32(row["GroupStep"]) + 1);
                result.Add(dto);
            }

            return result;
        }
        static List<CM_ProductGroupDTO> GetChildGroup(int parentid, int groupstep)
        {
            CM_ProductGroupBUS bus = new CM_ProductGroupBUS();
            List<CM_ProductGroupDTO> result = new List<CM_ProductGroupDTO>();
            DataTable source = bus.GetChildGroup(parentid, groupstep, userLogin.UserId);
            foreach (DataRow row in source.Rows)
            {
                CM_ProductGroupDTO dto = new CM_ProductGroupDTO();
                dto.ProductGroupName = row["ProductGroupName"] + "";
                dto.ProductGroupID = Convert.ToInt32(row["ProductGroupID"]);
                dto.children = GetChildGroup(dto.ProductGroupID, Convert.ToInt32(row["GroupStep"]) + 1);
                result.Add(dto);
            }
            return result;
        }
    }
}